#include "stdafx.h"
#include "Flying.h"

namespace MyGame {

Flying::Flying(Game* game) : Mesh(game) {
    this->acceleration = 0;
    this->direction = D3DXVECTOR3(0, 0, 0);
    this->isAccelerating = false;
    this->isDecelerating = false;
    this->maxAcceleration = 0;
    this->maxSpeed = 0;
    this->minAcceleration = 0;
    this->minSpeed = 0;
    this->modelDirection = D3DXVECTOR3(0, 0, 0);
    this->prevPosition = D3DXVECTOR3(0, 0, 0);
    this->speed = 0;
    this->SetState(FLYINGSTATE_ALIVE);
    this->turnSpeed = 0;
    this->usesFuel = false;
}

Flying::~Flying(void) {
}

FLOAT Flying::GetAcceleration() const {
    return this->acceleration;
}

void Flying::SetAcceleration(FLOAT val) {
    if (val < this->minAcceleration) {
        this->acceleration = this->minAcceleration;
    } else if (val > this->maxAcceleration) {
        this->acceleration = this->maxAcceleration;
    } else {
        this->acceleration = val;
    }
}

D3DXVECTOR3 Flying::GetDirection() const {
    return this->direction;
}

void Flying::SetDirection(D3DXVECTOR3 val) {
    this->direction = val;
}

FLOAT Flying::GetMaxAcceleration() const {
    return this->maxAcceleration;
}

void Flying::SetMaxAcceleration(FLOAT val) {
    if (val < this->minAcceleration) {
        this->maxAcceleration = this->minAcceleration;
    } else {
        this->maxAcceleration = val;
    }
}

FLOAT Flying::GetMaxSpeed() const {
    return this->maxSpeed;
}

void Flying::SetMaxSpeed(FLOAT val) {
    if (val < this->minSpeed) {
        this->maxSpeed = this->minSpeed;
    } else {
        this->maxSpeed = val;
    }
}

FLOAT Flying::GetMinAcceleration() const {
    return this->minAcceleration;
}

void Flying::SetMinAccelration(FLOAT val) {
    if (val > this->maxAcceleration) {
        this->minAcceleration = this->maxAcceleration;
    } else {
        this->minAcceleration = val;
    }
}

FLOAT Flying::GetMinSpeed() const {
    return this->minSpeed;
}

void Flying::SetMinSpeed(FLOAT val) {
    if (val > this->maxSpeed) {
        this->minSpeed = this->maxSpeed;
    } else {
        this->minSpeed = val;
    }
}

D3DXVECTOR3 Flying::GetModelDirection() const {
    return this->modelDirection;
}

void Flying::SetModelDirection(D3DXVECTOR3 val) {
    this->modelDirection = val;
}

D3DXVECTOR3 Flying::GetPrevPosition() const {
    return this->prevPosition;
}

FLOAT Flying::GetSpeed() const {
    return this->speed;
}

void Flying::SetSpeed(FLOAT val) {
    if (val < this->minSpeed) {
        this->speed = this->minSpeed;
    } else if (val > this->maxSpeed) {
        this->speed = this->maxSpeed;
    } else {
        this->speed = val;
    }
}

FlyingState Flying::GetState() const {
    return this->state;
}

void Flying::SetState(FlyingState val) {
    this->state = val;
    switch (this->state) {
    case FLYINGSTATE_ALIVE:
        this->timer = 10;
        break;
    case FLYINGSTATE_DYING:
        this->SetVisible(false);
        this->timer = 5;
        break;
    }
}

FLOAT Flying::GetTurnSpeed() const {
    return this->turnSpeed;
}

void Flying::SetTurnSpeed(FLOAT val) {
    this->turnSpeed = val;
}

bool Flying::GetUsesFuel() const {
    return this->usesFuel;
}

void Flying::SetUsesFuel(bool val) {
    this->usesFuel = val;
}

void Flying::Accelerate() {
    this->isAccelerating = true;
}

void Flying::Decelerate() {
    this->isDecelerating = true;
}

void Flying::Explode() {
    this->SetState(FLYINGSTATE_DYING);
}

void Flying::Update(FLOAT timeDelta) {
    if (this->timer > 0) {
        this->timer -= timeDelta;
        if (timer <= 0) {
            switch (this->state) {
            case FLYINGSTATE_ALIVE:
                if (this->usesFuel) {
                    this->SetState(FLYINGSTATE_DEAD);
                }

                break;
            case FLYINGSTATE_DYING:
                this->SetState(FLYINGSTATE_DEAD);
                break;
            }
        }
    }

    FLOAT a = 0;
    if (this->isAccelerating && !this->isDecelerating) {
        this->isAccelerating = false;
        a = this->acceleration;
    } else if (this->isDecelerating && !this->isAccelerating) {
        this->isDecelerating = false;
        a = -this->acceleration;
    } else if (this->isAccelerating && this->isDecelerating) {
        this->isAccelerating = false;
        this->isDecelerating = false;
    }

    this->SetSpeed(this->speed + a * timeDelta);
    D3DXVECTOR3 position = this->GetPosition();
    this->prevPosition = position;
    position += this->direction * this->speed * timeDelta;
    this->SetPosition(position);
}

}
